package com.monco.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monco.common.response.ApiResult;
import com.monco.entity.Dict;
import com.monco.service.IDictService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author com/monco
* @since 2020-03-09
*/
@Api(tags = "字典模块API")
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private IDictService dictService;

    /**
     * 新增Dict方法
     *
     * @param dict
     * @return
     */
    @PostMapping
    public ApiResult save(@RequestBody Dict dict) {
        dictService.save(dict);
        return ApiResult.ok();
    }

    /**
     * 修改Dict方法
     *
     * @param dict
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody Dict dict) {
        dictService.updateById(dict);
        return ApiResult.ok();
    }

    /**
     * 查询列表
     *
     * @param dict
     * @return
     */
    @GetMapping("list")
    public ApiResult list(Dict dict) {
        return ApiResult.ok(dictService.getDictList(dict));
    }

    /**
     * 查询分页
     *
     * @param current 当前页
     * @param size    一页多少数据
     * @param dict
     * @return
     */
    @GetMapping("page")
    public ApiResult page(@RequestParam(required = false, defaultValue = "0") long current,
                          @RequestParam(required = false, defaultValue = "10") long size, Dict dict) {
        return ApiResult.ok(dictService.getDictPage(new Page<Dict>().setCurrent(current).setSize(size), dict));
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ApiResult delete(@RequestParam Long id) {
        dictService.removeById(id);
        return ApiResult.ok();
    }

    /**
     * 按照id获取单条记录
     *
     * @param id
     * @return
     */
    @GetMapping
    public ApiResult getOne(@RequestParam Long id) {
        return ApiResult.ok(dictService.getById(id));
    }
}
